package com.example.demonstrate.wang.controller;

import com.example.demonstrate.wang.entity.PushFaceLog;
import com.example.demonstrate.wang.entity.PushProfileLog;
import com.example.demonstrate.wang.task.AllProfileFaceTask;
import com.example.demonstrate.wang.task.AllProfileTask;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.example.demonstrate.wang.constants.Constant;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author along
 * @Date 2024/6/27 14:46
 */

@RestController
public class OperationController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    private final AllProfileTask allProfileTask;

    private final AllProfileFaceTask allProfileFaceTask;

    public OperationController(RedisTemplate<String, Object> redisTemplate, MongoTemplate mongoTemplate, AllProfileTask allProfileTask, AllProfileFaceTask allProfileFaceTask) {
        this.redisTemplate = redisTemplate;
        this.mongoTemplate = mongoTemplate;
        this.allProfileTask = allProfileTask;
        this.allProfileFaceTask = allProfileFaceTask;
    }

    @ApiOperation(value = "全量档案抓拍重新同步", produces = "application/json")
    @GetMapping(value = {"/sync/face/{startDate}/{endDate}"})
    public String wholeProfileFacePushAsync(@PathVariable @ApiParam(name = "startDate", value = "格式:2024-01-01-00:00:00") String startDate,
                                            @PathVariable @ApiParam(name = "startDate", value = "格式:2024-01-01-00:00:00") String endDate) {
        // 清除redis cursor
        redisTemplate.delete(Constant.FACE_CURSOR);
        // 清除mongo 推送日志
        mongoTemplate.dropCollection(PushFaceLog.class);
        allProfileFaceTask.wholeProfileFacePushAsync();
        return "异步档案抓拍全量同步开始，观察系统日志";
    }

    @ApiOperation(value = "全量档案重新同步", produces = "application/json")
    @GetMapping(value = {"/sync/profile/{startDate}/{endDate}"})
    public String wholeProfilePushAsync(@PathVariable @ApiParam(name = "startDate", value = "格式:2024-01-01-00:00:00") String startDate,
                                            @PathVariable @ApiParam(name = "startDate", value = "格式:2024-01-01-00:00:00") String endDate) {
        // 清除redis cursor
        redisTemplate.delete(Constant.PROFILE_CURSOR);
        // 清除mongo 推送日志
        mongoTemplate.dropCollection(PushProfileLog.class);
        allProfileTask.wholeProfilePushAsync();
        return "异步档案全量同步开始，观察系统日志";
    }
}
